var fs = require("fs");
var path = require("path");

var imgExt = ["jpg", "jpeg", "gif", "png", "bmp"];
var videoExt = ["avi", "rmvb", "rm", "asf", "divx", "mpg", "mpeg", "mpe", "wmv", "mp4", "mkv", "vob", "flv"];
var removeExt = ["torrent", "lnk", "html", "htm"];


fileUtils = {
    sep: path.sep,

    formatFileSize: function (size) {
        var g = 1024 * 1024 * 1024;
        var m = 1024 * 1024;
        var k = 1024;
        if (size > g) {
            size = (Math.round(size * 100 / g) / 100) + "G";
        } else if (size > m) {
            size = (Math.round(size * 100 / m) / 100) + "M";
        } else if (size > k) {
            size = (Math.round(size * 100 / k) / 100) + "K";
        } else {
            size = size + "B";
        }
        return size;
    },

    formatFilePath: function (filePath) {
        return path.normalize(filePath);
    },

    isImg: function (filePath) {
        var ext = new PFile(filePath).extName;
        return imgExt.indexOf(ext.toLocaleLowerCase()) != -1;
    },

    isVideo: function (filePath) {
        var ext = new PFile(filePath).extName;
        return videoExt.indexOf(ext.toLocaleLowerCase()) != -1;
    },

    isRemove: function (filePath) {
        var ext = new PFile(filePath).extName;
        return removeExt.indexOf(ext.toLocaleLowerCase()) != -1;
    }
}


/**
 * 文件类
 *
 */
function PFile(filePath) {
    if (!filePath) {
        throw "路径为空，无法创建文件";
    }

    filePath = fileUtils.formatFilePath(filePath);

    var dirPath = path.dirname(filePath);
    var fileName = path.basename(filePath);
    var extName = path.extname(filePath);

    var baseName = fileName;

    var p = fileName.lastIndexOf(".");

    if (p > 0 && p != fileName.length) {
        baseName = fileName.substring(0, p);
        extName = fileName.substring(p + 1, fileName.length);
    }


    this.path = filePath;
    this.dirPath = dirPath;
    this.fileName = fileName;
    this.baseName = baseName;
    this.extName = extName;

    this.isExist = fs.existsSync(filePath);
    if (this.isExist) {
        this.isDir = fs.lstatSync(filePath).isDirectory();
        this.isFile = fs.lstatSync(filePath).isFile();
        this.size = fs.lstatSync(filePath).size;
        this.sizeInfo = fileUtils.formatFileSize(this.size);

    }
}

//删除该文件
PFile.prototype.delete = function () {
    fs.unlinkSync(this.path);
}


PFile.prototype.openInOS = function () {
    nw.Shell.openItem(this.path);
}

//获取子目录下全部文件
PFile.prototype.getAllSon = function () {
    if (!this.isDir) {
        return [];
    }
    var fileNames = fs.readdirSync(this.path);

    for (var i = 0; i < fileNames.length; i++) {
        fileNames[i] = new PFile(this.path + "/" + fileNames[i]).path;
    }
    return fileNames;
}
//获取目录下文件，只包括文件，不包括目录
PFile.prototype.getOnlyFile = function () {
    var fileNames = this.getAllSon();
    var result = [];
    for (var i = 0; i < fileNames.length; i++) {
        if (new PFile(fileNames[i]).isFile) {
            result.push(fileNames[i]);
        }
    }
    return result;
}
//获取子目录，只包括子目录
PFile.prototype.getOnlyDir = function () {
    var fileNames = this.getAllSon();
    var result = [];
    for (var i = 0; i < fileNames.length; i++) {
        if (new PFile(fileNames[i]).isDir) {
            result.push(fileNames[i]);
        }
    }
    return result;
}

//获取子目录，只包括子目录
PFile.prototype.getImage = function () {
    var fileNames = this.getOnlyFile();
    var result = [];
    for (var i = 0; i < fileNames.length; i++) {
        if (fileUtils.isImg(fileNames[i])) {
            result.push(fileNames[i]);
        }
    }
    return result;
}


